package com.woniu.intlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.client.OrderClient;
import com.woniu.command.proplancommand.AddProPlanCommand;
import com.woniu.command.proplancommand.DeleteProPlanCommand;
import com.woniu.command.proplancommand.UpdateProPlanCommand;
import com.woniu.intlet.web.jsr303.PurchasePoJsr;
import com.woniu.outlet.po.ProPlanPo;
import com.woniu.service.ProPlanPoService;
import com.woniu.uitl.JwtTokenUitl;
import com.woniu.uitl.ResponseResult;
import com.woniu.uitl.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@Api(tags = "生产计划接口文档")
public class ProPlanController {

    @Autowired
    private ProPlanPoService proPlanPoService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("proPlans")
    @ApiOperation("生产计划列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单id"),
            @ApiImplicitParam(name = "status",value = "生产计划表状态"),
    })
    public ResponseResult<List<ProPlanPo>> findAllProPlan(Long orderId,String status){
        List<ProPlanPo> poList = proPlanPoService.findAllProPlan(orderId,status);
        return new ResponseResult<List<ProPlanPo>>(poList,"OK", ResultCode.SUCCESS.getCode());
    }

    @PostMapping("addProPlan")
    @ApiOperation("添加生产计划")
    @ApiOperationSupport(ignoreParameters = {"id","planNo","spare3","createPerson","createTime",
                                            "fixPerson","fixTime","spare2","status"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contacts",required = true),
            @ApiImplicitParam(name = "contactsTel",required = true),
            @ApiImplicitParam(name = "customer",required = true),
            @ApiImplicitParam(name = "dueday",required = true),
            @ApiImplicitParam(name = "endTime",required = true),
            @ApiImplicitParam(name = "keyPoint",required = true),
            @ApiImplicitParam(name = "orderId",required = true),
            @ApiImplicitParam(name = "remark",required = true),
            @ApiImplicitParam(name = "spare1",required = true),
            @ApiImplicitParam(name = "startTime",required = true),
    })
    public ResponseResult<String> addProPlan(@RequestHeader(name = "redisToken") String redisToken,@RequestHeader(name = "tokenStr") String tokenStr,ProPlanPo po){
        if (redisTemplate.delete(redisToken)){
            //创建人
            String userName = JwtTokenUitl.getUserName(tokenStr);
            po.setCreatePerson(userName);
            AddProPlanCommand comm = new AddProPlanCommand();
            String msg = comm.excute(po);
            return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
        }
        return new ResponseResult<String>(500,"请不要频繁添加");
    }

    @PutMapping("updateProPlan")
    @ApiOperation("修改生产计划信息")
    @ApiOperationSupport(ignoreParameters = {"planNo","spare3","status","createPerson","createTime",
            "fixPerson","fixTime","spare2"})
    public ResponseResult<Map> updateProPlan(@Validated(value = PurchasePoJsr.class) ProPlanPo po, BindingResult result, @RequestHeader(name = "tokenStr") String tokenStr){
        HashMap<String,String> map = new HashMap<>();
        if (result.hasErrors()){
            result.getFieldErrors().forEach(item ->{
                map.put(item.getField(),item.getDefaultMessage());
            });
            return new ResponseResult<Map>(ResultCode.PARAM_NOT_VALID.getCode(),"参数不合法",map);
        }
        String userName = JwtTokenUitl.getUserName(tokenStr);
        po.setFixPerson(userName);
        po.setFixTime(new Date());
        UpdateProPlanCommand command = new UpdateProPlanCommand();
        String msg = command.excute(po);
        map.put("OK",msg);
        return new ResponseResult<Map>(ResultCode.SUCCESS.getCode(),msg);
    }

    @DeleteMapping("deleteProPlan/{id}")
    @ApiOperation("删除生产计划单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true,value = "生产计划单id")
    })
    public ResponseResult<String> deleteProPlanById(@PathVariable("id") Long id){
        DeleteProPlanCommand command = new DeleteProPlanCommand();
        String msg = command.excute(id);
        return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
    }

    @GetMapping("getProPlanDeta")
    @ApiOperation("生产计划单详情")
//    @PreAuthorize("hasAnyAuthority('drug:down')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "proId",required = true,value = "生产计划单id")
    })
    public ResponseResult<HashMap<String,Object>> getProById(Long proId){
        HashMap<String,Object> map = proPlanPoService.getProDetailById(proId);
        return new ResponseResult<HashMap<String,Object>>(map,"OK",ResultCode.SUCCESS.getCode());
    }
}
